package com.jeegit.ecms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jeegit.common.annotation.Log;
import com.jeegit.common.enums.BusinessType;
import com.jeegit.ecms.domain.CmsArticleCategory;
import com.jeegit.ecms.service.ICmsArticleCategoryService;
import com.jeegit.ecms.utils.ECMSUtils;
import com.jeegit.common.core.controller.BaseController;
import com.jeegit.common.core.domain.AjaxResult;
import com.jeegit.common.utils.poi.ExcelUtil;
import com.jeegit.common.utils.StringUtils;
import com.jeegit.common.core.domain.Ztree;

/**
 * 文章分类Controller
 * 
 * @author jeegit
 * @date 2022-06-25
 */
@Controller
@RequestMapping("/ecms/articlecategory")
public class CmsArticleCategoryController extends BaseController
{
    private String prefix = "ecms/articlecategory";

    @Autowired
    private ICmsArticleCategoryService cmsArticleCategoryService;

    @RequiresPermissions("ecms:articlecategory:view")
    @GetMapping()
    public String articlecategory(CmsArticleCategory cmsArticleCategory, ModelMap mmap)
    {
    	mmap.put("cmsArticleCategory", cmsArticleCategory);
    	mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
    	
    	
        return prefix + "/articlecategory";
    }

    /**
     * 查询文章分类树列表
     */
    @RequiresPermissions("ecms:articlecategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CmsArticleCategory> list(CmsArticleCategory cmsArticleCategory)
    {
        List<CmsArticleCategory> list = cmsArticleCategoryService.selectCmsArticleCategoryList(cmsArticleCategory);
        return list;
    }

    /**
     * 导出文章分类列表
     */
    @RequiresPermissions("ecms:articlecategory:export")
    @Log(title = "文章分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsArticleCategory cmsArticleCategory)
    {
        List<CmsArticleCategory> list = cmsArticleCategoryService.selectCmsArticleCategoryList(cmsArticleCategory);
        ExcelUtil<CmsArticleCategory> util = new ExcelUtil<CmsArticleCategory>(CmsArticleCategory.class);
        return util.exportExcel(list, "文章分类数据");
    }

    /**
     * 新增文章分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsArticleCategory", cmsArticleCategoryService.selectCmsArticleCategoryById(id));
        }
        mmap.put("cmsArticleCategory", new CmsArticleCategory());
        mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/add";
    }

    /**
     * 新增保存文章分类
     */
    @RequiresPermissions("ecms:articlecategory:add")
    @Log(title = "文章分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsArticleCategory cmsArticleCategory)
    {
        return toAjax(cmsArticleCategoryService.insertCmsArticleCategory(cmsArticleCategory));
    }

    /**
     * 修改文章分类
     */
    @RequiresPermissions("ecms:articlecategory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsArticleCategory cmsArticleCategory = cmsArticleCategoryService.selectCmsArticleCategoryById(id);
        //初始化树结构
        ECMSUtils.initECMSTreeData(cmsArticleCategory);
        mmap.put("cmsArticleCategory", cmsArticleCategory);
        mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/edit";
    }

    /**
     * 修改保存文章分类
     */
    @RequiresPermissions("ecms:articlecategory:edit")
    @Log(title = "文章分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsArticleCategory cmsArticleCategory)
    {
    	System.out.println("getParentId:"+cmsArticleCategory.getParentId());
        return toAjax(cmsArticleCategoryService.updateCmsArticleCategory(cmsArticleCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("ecms:articlecategory:remove")
    @Log(title = "文章分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cmsArticleCategoryService.deleteCmsArticleCategoryById(id));
    }

    /**
     * 选择文章分类树
     */
    @GetMapping(value = { "/selectArticlecategoryTree/{id}", "/selectArticlecategoryTree/" })
    public String selectArticlecategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsArticleCategory", cmsArticleCategoryService.selectCmsArticleCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载文章分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = cmsArticleCategoryService.selectCmsArticleCategoryTree();
        return ztrees;
    }
}
